﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office;
using Word = Microsoft.Office.Interop.Word;
using Excel;

namespace UsasCmsCommon
{
    public class OfficeConvert
    {

        public void ConvertEXCEL(string oFileName, string _HtmlFileName)
        {
    
            object missing = System.Reflection.Missing.Value;
            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook oBook = null;
            try
            {
                oBook = excelApp.Workbooks.Open(oFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                oBook.SaveAs(_HtmlFileName, Excel.XlFileFormat.xlHtml, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
                //需要注意两个参数，一个是Excel.XlFileFormat.xlHtml，一个是Excel.XlSaveAsAccessMode.xlNoChange
                //参数如果传递错误通常会引发一个COMException，说"Exception from HRESULT: 0x800A03EC."
                oBook.Close(false, missing, missing);
            }
            catch 
            {
                //throw new Exception(ex.Message);
                //display("Err:" + ex.Message);
            }
            finally
            {
                if (oBook != null)
                {
                    oBook.Close(false, missing, missing);
                }
                excelApp.Quit();
                GC.Collect();
            }
        }

        /// <summary>
        /// 将Excel文件转换成HTML格式
        /// </summary>
        /// <param name="ExcelFilePath">Excel文件路径</param>
        public void ExcelToHtmlFile(string ExcelFilePath)
        {
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook oBook = null;
            // 缺省参数 
            object Unknown = Type.Missing;
            try
            {
                //目标html文件路径
                object Target = ExcelFilePath.Substring(0, ExcelFilePath.LastIndexOf(".")) + ".html";
                //为了保险,只读方式打开 
                object readOnly = true;
                // 指定另存为格式(html) 
                object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

                //打开Excel文件
                oBook = excelApp.Workbooks.Open(ExcelFilePath, Unknown, readOnly,
                    Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,
                    Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);

                // 转换格式 
                oBook.SaveAs(Target, format, Unknown, Unknown, Unknown, Unknown,
                     Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                     Unknown, Unknown, Unknown, Unknown, Unknown);
            }
            finally
            {
                if (oBook != null)
                {
                    oBook.Close(false, Unknown, Unknown);
                }
                excelApp.Quit();
                GC.Collect();
            }
        }

        #region Excel转换成html
        /**/
        /// <summary>
        /// Excel转换成html
        /// </summary>
        /// <param name="ExcelFile">Excel路径名称</param>
        /// <param name="HTMLFile">Html路径名称</param>
        /// <returns></returns>
        public bool ExcelToHTML(string ExcelFile, string HTMLFile)
        {

            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Application app2 = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                app.Visible = false;
                app.DisplayAlerts = false;
                app2.Visible = false;
                app2.DisplayAlerts = false;
                //打开模板文件，得到WorkBook对象 
                int i = 0;

                Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(ExcelFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                foreach (Microsoft.Office.Interop.Excel.Workbook wss in workBook.Worksheets)
                {
                    Microsoft.Office.Interop.Excel.Workbook wbb = app2.Workbooks.Open(ExcelFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    Microsoft.Office.Interop.Excel.Workbook wb;
                    wb = wbb;
                    foreach (Microsoft.Office.Interop.Excel.Workbook ws in wb.Worksheets)
                    {
                        if (ws.Name != wss.Name)
                            ws.DeleteNumberFormat(ws.Name);
                    }
                    i++;
                    wb.SaveAs(HTMLFile + "_" + i.ToString() + ".htm", XlFileFormat.xlHtml, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing);
                    wbb.Close(false, Type.Missing, Type.Missing);
                    wbb = null;
                }
                workBook.Close(false, Type.Missing, Type.Missing);
                workBook = null;
                return true;
            }
            catch (Exception er)
            {
                
                return false;
            }
            finally
            {
                app.Workbooks.Close();
                app.Quit();
                app2.Workbooks.Close();
                app2.Quit();
                app = null;
                app2 = null;
                System.GC.Collect();
            }

        }
        #endregion


        public void ConvertWORD(string oFileName, string _HtmlFileName)
        {

            object oMissing = System.Reflection.Missing.Value;
            object True = true;
            object False = false;
            object fmt = Word.WdSaveFormat.wdFormatHTML;
            object strHtml = _HtmlFileName;


            Word.Application WordApp = new Word.Application();
            Word._Document oDoc = null;
            WordApp = new Word.Application();
            WordApp.Visible = false;
            object fileName = oFileName;
            try
            {

                oDoc = WordApp.Documents.Open(ref fileName,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                oDoc.SaveAs(ref strHtml, ref fmt, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);



                oDoc.Close(ref False, ref oMissing, ref oMissing);
                WordApp.Quit(ref False, ref oMissing, ref oMissing);
                GC.Collect();
            }

            catch (Exception ex)
            {



                if (oDoc != null)
                {
                    oDoc.Close(ref False, ref oMissing, ref oMissing);
                }


                WordApp.Quit(ref False, ref oMissing, ref oMissing);
                GC.Collect();
                throw new Exception(ex.Message);

            }


        }


    }
}
